package com.song.JavaThread_ch3_8;

import java.util.List;
import java.util.concurrent.Exchanger;

public class Producer implements Runnable {

	private List<String> buffer;
	
	private final Exchanger<List<String>> exchanger;
	
	
	
	public Producer(List<String> buffer, Exchanger<List<String>> exchanger) {
		super();
		this.buffer = buffer;
		this.exchanger = exchanger;
	}



	@Override
	public void run() {
		int cycle = 1;
		
		for(int i = 0 ;i<10 ;i++){
			
			System.out.printf("Producer :cycle %d \n" , cycle);
			for( int j = 0 ;j<10 ;j++){
				String message = "Event "+((i*10) +j);
				System.out.printf("Producer : %s \n" , message);
				buffer.add(message);
			}
			try {
				buffer = exchanger.exchange(buffer);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.printf("Producer : buffer size is %s \n" ,buffer.size());
			cycle++;
		}
	}
	
	
	
}
